/**
 *kuwang
 * 2019年9月25日
 */
package com.yidu.privilege.Action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.yidu.hbatsft.domain.*;
import com.yidu.hbatsft.service.AtsEmpAthleteService;
import com.yidu.hbatsft.service.AtsEmpCoachService;
import com.yidu.hbatsft.service.AtsEmpExpertService;
import com.yidu.hbatsft.service.AtsEmpLeaderService;
import com.yidu.privilege.Domain.User;
import com.yidu.privilege.Domain.UserAndrole;
import com.yidu.privilege.Service.UserService;
import com.yidu.privilege.Utils.BaseMessage;
import com.yidu.privilege.Utils.Pages;
import com.yidu.privilege.vo.RoleVo;
import com.yidu.privilege.vo.UserVo;
import com.yidu.utils.FileTools;
import com.yidu.utils.Message;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author kuwang
 *
 * 2019年9月25日 上午9:25:32
 */

@Controller
@RequestMapping("/UserAction")
public class UserController {
	Logger logger = Logger.getLogger(getClass());
	@Resource
	private UserService userService ;
	/**
	 * 领导service
	 */
	@Resource
	private AtsEmpLeaderService atsEmpLeaderService;


	/**
	 * 教练员service
	 */
	@Resource
	private AtsEmpCoachService atsEmpCoachService;

	/**
	 * 运动员service
	 */
	@Resource
	private AtsEmpAthleteService atsEmpAthleteService;

	/**
	 * 专家service
	 */
	@Resource
	private AtsEmpExpertService atsEmpExpertService;

	/**
	 *文件上传管理工具
	 */
	//文件上传管理工具
	@Resource
	FileTools fileTools;


	/**
	 * 查询所有
	 * @param pages 分页
	 * @param vo 用户
	 * @return
	 */
	@RequestMapping("finAll")
	@ResponseBody
	public Map<String,Object> finAll(Pages pages, UserVo vo){

		Map<String, Object> map = new HashMap<String, Object>();
		
		List<UserVo> list=userService.finAll(vo, pages);
		Long hang =userService.count(vo);
		
		map.put("code", 0);
		//给map的 msg为""
		map.put("msg", "");
		//给map的count弄成hang数
		map.put("count", hang);
		//给map的data传数据list
		map.put("data", list);
		//将分页的数据放入到map
		
		return map;
	}


    /**
     * 修改密码
     * @param
     * @return
     */
    @RequestMapping("gai")
    @ResponseBody
    public Message gai(String usid,String password1,String password2,String password3) {
        Message message = new Message();
        UserVo vo =userService.finByid(usid);
        System.out.println(vo.getUsPass());
        if (!vo.getUsPass().equals(password1)){
            message.setNum(1);
            message.setMsg("旧密码不正确");
        }else {
            if(!password2.equals(password3)){
                message.setNum(1);
                message.setMsg("两次密码不一样");
            }else {
                if (!vo.getUsPass().equals(password2)){
                    vo.setUsPass(password2);
                    int s= userService.add(vo,1);
                    if(s>0){
                        message.setNum(0);
                        message.setMsg("修改成功,请重新登录");
                    }else {
                        message.setNum(1);
                        message.setMsg("修改失败");
                    }
                }else {
                    message.setNum(1);
                    message.setMsg("新旧密码不能一样");
                }

            }
        }
        return  message;
    }


    /**
     * 退出成功跳转
     * @param session 移除上次登录的信息
     * @return
     */
    @RequestMapping("/unLogin")
    @ResponseBody
    public String unLogin(HttpSession session) {
        session.invalidate();
        return "redirect:../jurisdiction/Pages/index.html";
    }

    @RequestMapping("login")
	@ResponseBody
	public Message findname(String name,String pass, HttpSession session){
		System.out.println("登录中");
        UserVo vo=userService.finnamepsw(name,pass);
        Message message = new Message(0, "登录成功");

        if (vo.getUsId()!= null) {
            if (Integer.valueOf(vo.getUsState()) == 0) {
                session.setAttribute("manager", vo);
            } else {
                message.setNum(1);
                message.setMsg("账号无效");
            }
        } else {
            message.setNum(1);
            message.setMsg("用户名或密码错误");
        }
		return message;
	}

	/**
	 * 修改与删除
	 * @param uservo
	 * @param photo
	 * @param req
	 * @param ren
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("addorupdate")
	@ResponseBody
	public BaseMessage addorupdate(UserVo uservo, @RequestParam(value="file",required = false)MultipartFile photo, HttpServletRequest req,String ren)  throws IllegalStateException, IOException {


		if(photo.getOriginalFilename()!=null &&!"".equals(photo.getOriginalFilename())){
			try {
				String file1=fileTools.saveFile("UserImg",photo);
				uservo.setUsPhoto(file1);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String[] arr=ren.split(",");
		if(arr[0].equals("one")){
			uservo.setUsType("领导");
			uservo.setUsWaiid(Integer.valueOf(arr[1]));
		}else if(arr[0].equals("twe")){
			uservo.setUsType("教练");
			uservo.setUsWaiid(Integer.valueOf(arr[1]));
		}else if(arr[0].equals("three")){
			uservo.setUsType("运动员");
			uservo.setUsWaiid(Integer.valueOf(arr[1]));
		}else if(arr[0].equals("four")){
			uservo.setUsType("专家");
			uservo.setUsWaiid(Integer.valueOf(arr[1]));
		}else{
			uservo.setUsType("游客");
		}

		BaseMessage bm = new BaseMessage();
		

		userService.add(uservo,0);
		bm.setState(1);
		bm.setMsg("操作成功");
		return bm;
		
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public BaseMessage delete(String id) {

		BaseMessage bm = new BaseMessage();		

		userService.updatestate(id);
		bm.setState(1);
		bm.setMsg("删除成功");
		return bm;
		
		
	}
	@RequestMapping("finbyid")
	@ResponseBody
	public UserVo finbyid(String id){
		
		UserVo vo =userService.finByid(id);
		return vo;
	}
	
	@RequestMapping("finAllrole")
	@ResponseBody
	public List<RoleVo> finAll(RoleVo vo){

		
		List<RoleVo> list=userService.finAll(vo);
		
		return list;
	}

	@RequestMapping("fie")
	@ResponseBody
	public List<UserAndrole> fie(Integer id){
		
		List<UserAndrole> list=userService.finAll(id);
		
		return list;
	}
	
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("deleteAll")
	@ResponseBody
	public BaseMessage deleteAll(String ids) {
		
		BaseMessage bm = new BaseMessage();
		bm.setState(1);
		userService.deleteAll(ids);
		bm.setMsg("删除成功!");
		return bm ;
	}

	@RequestMapping("selctlet")
	@ResponseBody
	public List<Map<String,Object>> selctlet(String id){
		List<User> df=userService.finAll2();
		UserVo us=null;
			if (!StringUtils.isEmpty(id)){
				us=userService.finByid(id);
			}
		List<Map<String,Object>> as=new ArrayList<Map<String,Object>>();
		List<AtsEmpLeader> list=atsEmpLeaderService.findAllx();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("name","领导");
	List<Map<String,Object>> ast=new ArrayList<Map<String,Object>>();
		for (Iterator<AtsEmpLeader> iterator = list.iterator(); iterator.hasNext(); ) {
			AtsEmpLeader next =  iterator.next();
			Map<String,Object> map1=new HashMap<String,Object>();

			map1.put("name",next.getLeaderName());
			map1.put("value","one,"+next.getLeaderId());
			for (Iterator<User> iterator2 = df.iterator(); iterator2.hasNext(); ) {
				User next2 =  iterator2.next();
				if("领导".equals(next2.getUsType()) && next.getLeaderId()==next2.getUsWaiid()){

					map1.put("disabled",true);
				}

			}
			if(us!=null){
				if("领导".equals(us.getUsType()) && next.getLeaderId()==us.getUsWaiid()){
					map1.put("selected",true);
				}
			}
			ast.add(map1);

		}
		map.put("children",ast);
		as.add(map);


		Map<String,Object> map2=new HashMap<String,Object>();
		map2.put("name","教练");

		List<Map<String,Object>> ast1=new ArrayList<Map<String,Object>>();
		List<AtsEmpCoach> lift=atsEmpCoachService.findAll();
		for (Iterator<AtsEmpCoach> iterator = lift.iterator(); iterator.hasNext(); ) {
			AtsEmpCoach next =  iterator.next();
			Map<String,Object> map3=new HashMap<String,Object>();
			map3.put("name",next.getCoachName());
			map3.put("value","twe,"+next.getCoachId());
			for (Iterator<User> iterator2 = df.iterator(); iterator2.hasNext(); ) {
				User next2 =  iterator2.next();
				if("教练".equals(next2.getUsType()) && next.getCoachId()==next2.getUsWaiid()){
					map3.put("disabled",true);
				}

			}
			if(us!=null){
				if("教练".equals(us.getUsType()) && next.getCoachId()==us.getUsWaiid()){
					map3.put("selected",true);
				}
			}
			ast1.add(map3);
		}
		map2.put("children",ast1);
		as.add(map2);


		Map<String,Object> map4=new HashMap<String,Object>();
		map4.put("name","运动员");

		List<Map<String,Object>> ast2=new ArrayList<Map<String,Object>>();
		List<AtsEmpAthlete> lidt=atsEmpAthleteService.findAllx();
		for (AtsEmpAthlete atsEmpAthlete : lidt) {
			Map<String,Object> map5=new HashMap<String,Object>();
			map5.put("name",atsEmpAthlete.getAthleteName());
			map5.put("value","three,"+atsEmpAthlete.getAthleteId());
			for (Iterator<User> iterator2 = df.iterator(); iterator2.hasNext(); ) {
				User next2 =  iterator2.next();
				if("运动员".equals(next2.getUsType()) && atsEmpAthlete.getAthleteId()==next2.getUsWaiid()){
					map5.put("disabled",true);
				}

			}
			if(us!=null){
				if("运动员".equals(us.getUsType()) && atsEmpAthlete.getAthleteId()==us.getUsWaiid()){
					map5.put("selected",true);
				}
			}
			ast2.add(map5);
		}
		map4.put("children",ast2);
		as.add(map4);


		Map<String,Object> map7=new HashMap<String,Object>();
		map7.put("name","专家");
		List<Map<String,Object>> ast3=new ArrayList<Map<String,Object>>();
		List<AtsEmpExpert> liep=atsEmpExpertService.findAlls();

		for (AtsEmpExpert atsEmpExpert : liep) {
			Map<String,Object> map6=new HashMap<String,Object>();
			map6.put("name",atsEmpExpert.getExpertName());
			map6.put("value","four,"+atsEmpExpert.getExpertId());
			for (Iterator<User> iterator3 = df.iterator(); iterator3.hasNext(); ) {
				User next3 =  iterator3.next();
				if("专家".equals(next3.getUsType()) && atsEmpExpert.getExpertId()==next3.getUsWaiid()){
					map6.put("disabled",true);
				}

			}
			if(us!=null){
				if("专家".equals(us.getUsType()) && atsEmpExpert.getExpertId()==us.getUsWaiid()){
					map6.put("selected",true);
				}
			}
			ast3.add(map6);
		}
		map7.put("children",ast3);
		as.add(map7);
		return  as;
	}

    /**
     * 查询是否已经登录
     * @param session
     * @return
     */
    @RequestMapping("/findModByManager")
    @ResponseBody
    public UserVo findModByManager(HttpSession session) {

        UserVo man = (UserVo) session.getAttribute("manager");
        if(man==null){
            man = new UserVo();
            man.setUsId(0);
            man.setUsName("请重新登录");
        }
        System.out.println(man.getUsId());
        return man;
    }
}
